//
//  CJSTNDetailViewController.m
//  GoTraveling
//
//  Created by 陈金山 on 15/12/25.
//  Copyright © 2015年 开发华佗. All rights reserved.
//

#import "CJSTNDetailViewController.h"
#import "CJSTNDetailCell.h"
#import "CJSWaypoints.h"
#import "CJSTravelDetailheadView.h"
#import "CJSTravelDetailFootView.h"
#import "ZHFPhoto.h"

@interface CJSTNDetailViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, retain) CJSTravelDetailheadView *headView;

@property (nonatomic, retain) CJSTravelDetailFootView *footView;

@property (nonatomic, assign) BOOL isShowHeader;

@end

@implementation CJSTNDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //初始化
    self.index = self.row;
    self.isShowHeader = YES;
    
    //状态栏
    [UIApplication sharedApplication].statusBarHidden = YES;
    
    //collectionView
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = self.view.backgroundColor;
    self.collectionView.pagingEnabled = YES;
    self.collectionView.delegate = self;
    self.collectionView.bounces = NO;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[CJSTNDetailCell class] forCellWithReuseIdentifier:@"TNDetailItem"];
    [self.view addSubview:self.collectionView];
    
    //头
    self.headView = [[CJSTravelDetailheadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 44)];
    [self.view addSubview:self.headView];
    
    //尾
    self.footView = [[CJSTravelDetailFootView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 100, SCREEN_WIDTH, 100)];
    [self.view addSubview:self.footView];
    
    self.headView.pageNumber = [NSString stringWithFormat:@"%ld / %ld", self.index + 1, self.array.count];
    NSLog(@"1");
    self.footView.waypoint = [self.array objectAtIndex:self.index];
    
    
    //返回
    UIButton *returnBut = [UIButton buttonWithType:UIButtonTypeCustom];
    returnBut.frame = CGRectMake(20 * ADAPT_WIDTH, 10 * ADAPT_HEIGHT, 32 * ADAPT_WIDTH, 32 * ADAPT_HEIGHT);
    returnBut.tag = 1000;
    [returnBut setImage:[[UIImage imageNamed:@"iconfont-addcollapse.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
    returnBut.tintColor = [UIColor whiteColor];
    [returnBut addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnBut];
    
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.row inSection:10] atScrollPosition:UICollectionViewScrollPositionRight animated:NO];
}


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 20;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    
    
    return self.array.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    //头尾显示
    if (self.isShowHeader) {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.headView.frame = CGRectMake(0, -44, SCREEN_WIDTH, 44);
            self.footView.alpha = 0;
        }];
    }
    else {
        [UIView animateWithDuration:0.5 animations:^{
            
            self.headView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
            self.footView.alpha = 1;
        }];
    }
    self.isShowHeader = !self.isShowHeader;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CJSTNDetailCell *TNDetailCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TNDetailItem" forIndexPath:indexPath];
    
    CJSWaypoints *waypoint = [self.array objectAtIndex:indexPath.row];
    TNDetailCell.image_url = waypoint.photo;
    
    return TNDetailCell;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    CGFloat f = scrollView.contentOffset.x / SCREEN_WIDTH;
    self.index = (NSInteger)f % self.array.count;

    
    self.headView.pageNumber = [NSString stringWithFormat:@"%ld / %ld", self.index + 1, self.array.count];
    self.footView.waypoint = [self.array objectAtIndex:self.index];
    
}

- (void)buttonAction:(UIButton *)button{
    
    [UIApplication sharedApplication].statusBarHidden = NO;
    [self.delegate scrollCellWithIndex:self.index];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
